package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/url"
	"strconv"
)

type AddressInfoApi struct {
}

var addressInfoService = service.ServiceGroupApp.AutoCodeServiceGroup.AddressInfoService

// CreateAddressInfo 创建AddressInfo
// @Tags AddressInfo
// @Summary 创建AddressInfo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.AddressInfo true "创建AddressInfo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /addressInfo/createAddressInfo [post]
func (addressInfoApi *AddressInfoApi) CreateAddressInfo(c *gin.Context) {
	var addressInfo autocode.AddressInfo
	_ = c.ShouldBindJSON(&addressInfo)
	if err := addressInfoService.CreateAddressInfo(addressInfo); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteAddressInfo 删除AddressInfo
// @Tags AddressInfo
// @Summary 删除AddressInfo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.AddressInfo true "删除AddressInfo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /addressInfo/deleteAddressInfo [delete]
func (addressInfoApi *AddressInfoApi) DeleteAddressInfo(c *gin.Context) {
	var addressInfo autocode.AddressInfo
	_ = c.ShouldBindJSON(&addressInfo)
	if err := addressInfoService.DeleteAddressInfo(addressInfo); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteAddressInfoByIds 批量删除AddressInfo
// @Tags AddressInfo
// @Summary 批量删除AddressInfo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除AddressInfo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /addressInfo/deleteAddressInfoByIds [delete]
func (addressInfoApi *AddressInfoApi) DeleteAddressInfoByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := addressInfoService.DeleteAddressInfoByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateAddressInfo 更新AddressInfo
// @Tags AddressInfo
// @Summary 更新AddressInfo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.AddressInfo true "更新AddressInfo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /addressInfo/updateAddressInfo [put]
func (addressInfoApi *AddressInfoApi) UpdateAddressInfo(c *gin.Context) {
	var addressInfo autocode.AddressInfo
	_ = c.ShouldBindJSON(&addressInfo)
	if err := addressInfoService.UpdateAddressInfo(addressInfo); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindAddressInfo 用id查询AddressInfo
// @Tags AddressInfo
// @Summary 用id查询AddressInfo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.AddressInfo true "用id查询AddressInfo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /addressInfo/findAddressInfo [get]
func (addressInfoApi *AddressInfoApi) FindAddressInfo(c *gin.Context) {
	var addressInfo autocode.AddressInfo
	_ = c.ShouldBindQuery(&addressInfo)
	if err, readdressInfo := addressInfoService.GetAddressInfo(addressInfo.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"readdressInfo": readdressInfo}, c)
	}
}

// GetAddressInfoList 分页获取AddressInfo列表
// @Tags AddressInfo
// @Summary 分页获取AddressInfo列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.AddressInfoSearch true "分页获取AddressInfo列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /addressInfo/getAddressInfoList [get]
func (addressInfoApi *AddressInfoApi) GetAddressInfoList(c *gin.Context) {
	var pageInfo autocodeReq.AddressInfoSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := addressInfoService.GetAddressInfoInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetAddressInfoInfoListAll 获取AddressInfo列表
// @Tags AddressInfo
// @Summary 获取AddressInfo列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.AddressInfoSearch true "获取AddressInfo列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /addressInfo/GetAddressInfoInfoListAll [get]
func (addressInfoApi *AddressInfoApi) GetAddressInfoInfoListAll(c *gin.Context) {
	var pageInfo autocodeReq.AddressInfoSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list := addressInfoService.GetAddressInfoInfoListAll(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		if len(list) == 0 {
			escapeUrl := url.QueryEscape("导出失败")
			c.Header("msg", escapeUrl)
			global.GVA_LOG.Error("导出失败!", zap.Error(err))
			response.FailWithMessage("导出失败", c)
		} else {
			header := []string{"用户id", "用户名", "openId", "对应奖品", "收货人", "收货人电话", "地址", "区域信息", "创建时间"}
			data := [][]string{
				header,
			}
			for _, v := range list {
				context := []string{
					strconv.Itoa(*v.UserId),
					v.UserName,
					v.OpenId,
					v.GoodsName,
					v.Name,
					v.Phone,
					v.AddressName,
					v.Area,
					v.CreateTime.Format("2006/01/02 15:04:05"),
				}
				data = append(data, context)
			}
			if filename, err := utils.ExportFile(data); err != nil {
				global.GVA_LOG.Error("导出失败!", zap.Error(err))
				response.FailWithMessage("导出失败", c)
			} else {
				c.Header("success", "true")
				c.File(filename)
			}
		}
	}
}
